<?php
/**
 * 3-4岁专区认知内容管理页面
 */
require_once '../../../admin/check_session.php';
require_once '../../../includes/config.php';
require_once '../includes/CognitiveManager34.php';

// 创建认知管理器实例
$cognitiveManager = new CognitiveManager34();

// 检查模块ID参数
if (!isset($_GET['module_id'])) {
    // 如果没有提供模块ID，重定向到模块列表页面
    header("Location: cognitive_modules.php");
    exit;
}

$moduleId = (int)$_GET['module_id'];
$module = $cognitiveManager->getModuleById($moduleId);

// 验证模块存在且属于3-4岁年龄段
if (!$module) {
    // 如果模块不存在或不属于3-4岁年龄段，重定向到模块列表页面
    header("Location: cognitive_modules.php?error=invalid_module");
    exit;
}

// 处理删除操作
if (isset($_POST['delete_content']) && isset($_POST['content_id'])) {
    $contentId = (int)$_POST['content_id'];
    $result = $cognitiveManager->moduleManager->deleteContent($contentId);
    
    if ($result) {
        $successMessage = "内容项已成功删除";
    } else {
        $errorMessage = "删除内容项时出错";
    }
}

// 处理激活/停用操作
if (isset($_POST['toggle_status']) && isset($_POST['content_id'])) {
    $contentId = (int)$_POST['content_id'];
    $content = $cognitiveManager->moduleManager->getContentById($contentId);
    
    if ($content) {
        $newStatus = $content['is_active'] ? 0 : 1;
        $result = $cognitiveManager->moduleManager->updateContent($contentId, ['is_active' => $newStatus]);
        
        if ($result) {
            $successMessage = "内容状态已更新";
        } else {
            $errorMessage = "更新内容状态时出错";
        }
    }
}

// 获取模块下的所有内容
$contents = $cognitiveManager->getModuleContents($moduleId);

// 获取模块统计数据
$moduleStats = $cognitiveManager->getModuleStats($moduleId);

// 添加页面标题
$pageTitle = "管理 "{$module['module_name']}" 的内容";
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?> - KidsMind管理后台</title>
    <!-- 引入管理后台通用样式 -->
    <link rel="stylesheet" href="/admin/css/admin.css">
    <link rel="stylesheet" href="/admin/css/bootstrap.min.css">
    <link rel="stylesheet" href="/admin/css/fontawesome.min.css">
    <!-- 3-4岁专用样式 -->
    <style>
        .content-card {
            border-radius: 15px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            transition: all 0.3s ease;
            margin-bottom: 20px;
            border: none;
            overflow: hidden;
        }
        .content-card:hover {
            box-shadow: 0 8px 16px rgba(0,0,0,0.15);
            transform: translateY(-5px);
        }
        .content-header {
            padding: 15px;
            border-radius: 15px 15px 0 0;
        }
        .content-body {
            padding: 15px;
            background-color: #fff;
        }
        .badge-status {
            font-size: 0.8rem;
            padding: 5px 10px;
            border-radius: 20px;
        }
        .add-content-card {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 200px;
            background-color: #f8f9fa;
            border: 2px dashed #dee2e6;
            cursor: pointer;
            border-radius: 15px;
            transition: all 0.3s ease;
        }
        .add-content-card:hover {
            background-color: #e9ecef;
            border-color: #adb5bd;
        }
        .add-content-icon {
            font-size: 3rem;
            color: #6c757d;
        }
        .module-info {
            background-color: <?php echo getColorCode($module['background_color']); ?>;
            border-radius: 15px;
            padding: 20px;
            margin-bottom: 30px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .module-icon {
            font-size: 2.5rem;
            margin-right: 15px;
        }
        .stats-card {
            border-radius: 10px;
            padding: 15px;
            margin-bottom: 15px;
            background-color: #fff;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        }
        .stats-number {
            font-size: 2rem;
            font-weight: bold;
            color: #6c757d;
        }
        .stats-label {
            color: #6c757d;
            font-size: 0.9rem;
        }
        .color-preview {
            width: 20px;
            height: 20px;
            display: inline-block;
            border-radius: 50%;
            margin-right: 5px;
            vertical-align: middle;
        }
        /* 马卡龙色系配色 */
        .bg-pastel-pink { background-color: #FFD6E0; }
        .bg-pastel-blue { background-color: #D6E5FF; }
        .bg-pastel-green { background-color: #D6FFE1; }
        .bg-pastel-yellow { background-color: #FFEFD6; }
        .bg-pastel-purple { background-color: #E9D6FF; }
    </style>
</head>
<body>
    <?php include '../../../admin/includes/header.php'; ?>
    
    <div class="container-fluid">
        <div class="row">
            <?php include '../../../admin/includes/sidebar.php'; ?>
            
            <main class="col-md-9 ms-sm-auto col-lg-10 px-md-4">
                <div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
                    <h1 class="h2"><?php echo $pageTitle; ?></h1>
                    <div class="btn-toolbar mb-2 mb-md-0">
                        <div class="btn-group me-2">
                            <a href="cognitive_content_edit.php?module_id=<?php echo $moduleId; ?>" class="btn btn-sm btn-outline-primary">添加新内容</a>
                            <a href="cognitive_modules.php" class="btn btn-sm btn-outline-secondary">返回模块列表</a>
                        </div>
                    </div>
                </div>
                
                <?php if (isset($successMessage)): ?>
                <div class="alert alert-success" role="alert">
                    <?php echo $successMessage; ?>
                </div>
                <?php endif; ?>
                
                <?php if (isset($errorMessage)): ?>
                <div class="alert alert-danger" role="alert">
                    <?php echo $errorMessage; ?>
                </div>
                <?php endif; ?>
                
                <!-- 模块信息卡片 -->
                <div class="module-info">
                    <div class="row align-items-center">
                        <div class="col-md-6">
                            <div class="d-flex align-items-center">
                                <span class="module-icon"><?php echo $module['icon']; ?></span>
                                <div>
                                    <h3 class="mb-1"><?php echo htmlspecialchars($module['module_name']); ?></h3>
                                    <p class="mb-0 text-muted"><?php echo nl2br(htmlspecialchars($module['description'])); ?></p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="row">
                                <div class="col-4">
                                    <div class="stats-card text-center">
                                        <div class="stats-number"><?php echo $moduleStats['contents']; ?></div>
                                        <div class="stats-label">内容项</div>
                                    </div>
                                </div>
                                <div class="col-4">
                                    <div class="stats-card text-center">
                                        <div class="stats-number"><?php echo $moduleStats['examples']; ?></div>
                                        <div class="stats-label">示例</div>
                                    </div>
                                </div>
                                <div class="col-4">
                                    <div class="stats-card text-center">
                                        <div class="stats-number"><?php echo $moduleStats['encyclopedias']; ?></div>
                                        <div class="stats-label">百科</div>
                                    </div>
                                </div>
                            </div>
                            <div class="d-flex justify-content-end mt-2">
                                <a href="cognitive_module_edit.php?id=<?php echo $moduleId; ?>" class="btn btn-sm btn-outline-primary me-2">
                                    <i class="fas fa-edit"></i> 编辑模块
                                </a>
                                <a href="knowledge_encyclopedia.php?module_id=<?php echo $moduleId; ?>" class="btn btn-sm btn-outline-info">
                                    <i class="fas fa-book"></i> 管理百科
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="row">
                    <?php if (empty($contents)): ?>
                    <div class="col-12">
                        <div class="alert alert-info" role="alert">
                            <i class="fas fa-info-circle me-2"></i> 该模块下还没有内容。点击 "添加新内容" 按钮开始创建内容。
                        </div>
                    </div>
                    <?php endif; ?>
                    
                    <?php foreach ($contents as $content): ?>
                    <div class="col-md-4">
                        <div class="card content-card">
                            <div class="content-header bg-<?php echo getContentColor($content, $module); ?>">
                                <div class="d-flex justify-content-between align-items-center">
                                    <h5 class="mb-0">
                                        <?php if (!empty($content['color_code'])): ?>
                                        <span class="color-preview" style="background-color: <?php echo $content['color_code']; ?>"></span>
                                        <?php endif; ?>
                                        <?php echo htmlspecialchars($content['content_title']); ?>
                                    </h5>
                                    <?php if ($content['is_active']): ?>
                                    <span class="badge bg-success badge-status">已激活</span>
                                    <?php else: ?>
                                    <span class="badge bg-secondary badge-status">未激活</span>
                                    <?php endif; ?>
                                </div>
                            </div>
                            <div class="content-body">
                                <p class="text-muted mb-3"><?php echo nl2br(htmlspecialchars($content['description'] ?: '暂无描述')); ?></p>
                                
                                <div class="d-flex justify-content-between align-items-center mb-2">
                                    <small class="text-muted">更新于: <?php echo date('Y-m-d H:i', strtotime($content['updated_at'])); ?></small>
                                    <small class="text-muted">排序: <?php echo $content['content_order']; ?></small>
                                </div>
                                
                                <div class="btn-group w-100" role="group">
                                    <a href="cognitive_content_edit.php?id=<?php echo $content['content_id']; ?>&module_id=<?php echo $moduleId; ?>" class="btn btn-outline-primary btn-sm">
                                        <i class="fas fa-edit"></i> 编辑
                                    </a>
                                    <a href="content_examples.php?content_id=<?php echo $content['content_id']; ?>&module_id=<?php echo $moduleId; ?>" class="btn btn-outline-info btn-sm">
                                        <i class="fas fa-images"></i> 示例
                                    </a>
                                    <form method="post" onsubmit="return confirm('确定要<?php echo $content['is_active'] ? '停用' : '激活'; ?>该内容吗？');" style="display: inline;">
                                        <input type="hidden" name="content_id" value="<?php echo $content['content_id']; ?>">
                                        <button type="submit" name="toggle_status" class="btn btn-outline-<?php echo $content['is_active'] ? 'warning' : 'success'; ?> btn-sm">
                                            <i class="fas fa-<?php echo $content['is_active'] ? 'pause' : 'play'; ?>"></i> 
                                            <?php echo $content['is_active'] ? '停用' : '激活'; ?>
                                        </button>
                                    </form>
                                    <form method="post" onsubmit="return confirm('确定要删除该内容吗？此操作不可撤销，会同时删除所有关联的示例！');" style="display: inline;">
                                        <input type="hidden" name="content_id" value="<?php echo $content['content_id']; ?>">
                                        <button type="submit" name="delete_content" class="btn btn-outline-danger btn-sm">
                                            <i class="fas fa-trash"></i> 删除
                                        </button>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                    
                    <!-- 添加新内容卡片 -->
                    <div class="col-md-4">
                        <a href="cognitive_content_edit.php?module_id=<?php echo $moduleId; ?>" class="text-decoration-none">
                            <div class="add-content-card">
                                <div class="text-center">
                                    <div class="add-content-icon mb-2">
                                        <i class="fas fa-plus-circle"></i>
                                    </div>
                                    <h5 class="mb-0">添加新内容</h5>
                                </div>
                            </div>
                        </a>
                    </div>
                </div>
            </main>
        </div>
    </div>
    
    <script src="/admin/js/bootstrap.bundle.min.js"></script>
    <script src="/admin/js/jquery-3.6.0.min.js"></script>
</body>
</html>

<?php
/**
 * 根据颜色名称获取颜色代码
 */
function getColorCode($colorName) {
    $colorMap = [
        'pink' => '#FFD6E0',
        'blue' => '#D6E5FF',
        'green' => '#D6FFE1',
        'yellow' => '#FFEFD6',
        'purple' => '#E9D6FF'
    ];
    
    return isset($colorMap[$colorName]) ? $colorMap[$colorName] : '#FFD6E0';
}

/**
 * 根据内容和模块信息返回对应的色系类
 */
function getContentColor($content, $module) {
    // 使用模块默认颜色，使页面风格统一
    $colorMap = [
        'pink' => 'pastel-pink',
        'blue' => 'pastel-blue',
        'green' => 'pastel-green',
        'yellow' => 'pastel-yellow',
        'purple' => 'pastel-purple'
    ];
    
    if (isset($module['background_color']) && array_key_exists($module['background_color'], $colorMap)) {
        return $colorMap[$module['background_color']];
    }
    
    // 根据内容ID分配固定颜色，确保相同内容总是获得相同颜色
    $colors = array_values($colorMap);
    $index = $content['content_id'] % count($colors);
    return $colors[$index];
} 